// Andrew Gooch
// August 2022
// Repositioning Experiment
//Balance Tests

//INSTRUCTIONS:
// This do file assesses the effectiveness of the randomization. 
// To run code, change directory locations.
// All code can be run at once.
// All code uses the outsum and outreg2 functions, so you must install these before running.
// To install, type “net install outsum.pkg” and “net install outreg2.pkg” in the Stata command line. 

//BALANCE TESTS CONDUCTED:
// 1) Multinomial logit regression predicting treatment assignment
// 2) F-test assessing if covariates jointly predict treatment
// 3) Means and standard deviations outsum'ed to a table with f-test p-value
// 4) The goal is to show that variables do NOT predict treatment assignment (and therefore treatment was randomized correctly)


// LOAD DATA, DROP NON-CONSENT, DROP FAILED ATTENTION CHECK, AND CREATE TREATMENT VAR
clear all
set more off

//Change file directory to match your terminal
cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\ReplicationArchive\Data"
use Repositioning_ReplicationArchive

// drop people who did not consent
drop if q0 == "I do not agree to participate"

// drop people who failed the attention check
drop if q00 != "Cat" // 43 people failed

// create treatment var
gen treatment = .
replace treatment = 1 if q6_1 != .
replace treatment = 2 if q9_1 != .
replace treatment = 3 if q13_1 != .
replace treatment = 4 if q16_1 != .
replace treatment = 5 if q19_1 != .
replace treatment = 6 if q22_1 != .
label define treatment 1 "Campaign Position" 2 "Consistent" 3 "Reposition" 4 "Voters Response" 5 "Media Reponse" 6 "Activist Response" 
label values treatment treatment 
label var treatment "Treatment assignment"

// RECODE PRE-TREATMENT COVARIATES
// (A) Politically relevant survey questions used to predict treatment assignment
// Ideology
gen self_ideology = .
replace self_ideology = -2 if q1 =="Very Liberal"
replace self_ideology = -1 if q1 =="Liberal"
replace self_ideology = 0 if q1 == "Moderate" | q1=="Not sure"
replace self_ideology = 1 if q1 =="Conservative"
replace self_ideology = 2 if q1 =="Very Conservative"

//Voted vs not voted
gen turnout = .
replace turnout = 1 if q2 =="Yes, I definitely voted"
replace turnout = 0 if q2 =="I cannot recall if I voted or not" | q2 =="I usually vote, but did not in 2020" | q2 =="No, I did not vote" 

// Immigration is the most important issue (n=364)
gen immi_important = .
replace immi_important = 1 if q4 =="Immigration"
replace immi_important = 0 if q4 !="Immigration" 

//recode question
gen Trump_voter = 0
replace Trump_voter = 1 if q3 =="Donald Trump (Republican)"


// (B) Variables that come from Lucid (i.e., not questions asked in the survey)
// Codebook from Lucid is also included in replication files
// party ID
gen pid = .
replace pid = -1 if political_party == 1 | political_party == 2 | political_party == 3 | political_party ==6
replace pid = 0 if political_party == 4 | political_party == 7
replace pid = 1 if political_party == 5 | political_party == 8 | political_party == 9 | political_party==10

// Education includes missing data, so assigning mean edu to missing
//first create var without missing to get means, then replacing
gen education_no_miss = education
replace education_no_miss = . if education_no_miss == -3105
summ education_no_miss // mean = 4.498
replace education = 4.498 if education == -3105
replace education = 4.498 if education == 9

// Household income includes missing data, so assigning mean edu to missing
//first create var without missing to get means, then replacing
gen hhi_no_miss = hhi
replace hhi_no_miss = . if hhi_no_miss == -3105
summ hhi_no_miss // mean = 4.498
replace hhi = 9.489 if hhi == -3105

// race: white
gen white = 0
replace white = 1 if ethnicity == 1


//MAIN ANALYSIS
// multi logic regression with F-test
set more off 
mlogit treatment pid self_ideology turnout immi_important Trump_voter hhi education white 
testparm pid self_ideology turnout immi_important Trump_voter hhi education white 
local ftest = r(p) // p value = 0.5076

// Table of means and standard deviations
// Need to download outsum function to run this code
//Change directory to match your terminal
cd "C:\Users\Andrew Gooch\Dropbox\Working papers\Repositioning\ReplicationArchive\Data"
outsum pid self_ideology turnout immi_important Trump_voter hhi education white if treatment==1 using "BalanceTable.out" , replace ctitle ("Campaign Position") bracket addnote("F test p-value: `ftest'") 
outsum pid self_ideology turnout immi_important Trump_voter hhi education white if treatment==2 using "BalanceTable.out" , append ctitle ("Consistent in Office") bracket
outsum pid self_ideology turnout immi_important Trump_voter hhi education white if treatment==3 using "BalanceTable.out" , append ctitle ("Reposition in Office") bracket
outsum pid self_ideology turnout immi_important Trump_voter hhi education white if treatment==4 using "BalanceTable.out" , append ctitle ("Reposition + Voters Criticize") bracket
outsum pid self_ideology turnout immi_important Trump_voter hhi education white if treatment==5 using "BalanceTable.out" , append ctitle ("Reposition + Media Criticizes") bracket
outsum pid self_ideology turnout immi_important Trump_voter hhi education white if treatment==6 using "BalanceTable.out" , append ctitle ("Reposition + Actvists Criticize") bracket


